package com.education.springbootdemo;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.education.springbootdemo.exception.NoAuthExpetion;
import com.education.springbootdemo.utils.SessionUtils;
import com.education.springbootdemo.utils.TokenUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import java.lang.reflect.Method;
import java.util.Base64;
import java.util.Map;

//jwt
public class JWTInterceptor implements HandlerInterceptor {
    //构造函数传入Service
    public JWTInterceptor() {

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        if (handler instanceof HandlerMethod) {
//
//            String auth = request.getHeader("Authorization");
//            Map<String,Claim> m = TokenUtils.parseToken(auth);
//
//            Claim exp = m.get("exp");
//
//            Long expire = exp.asDate().getTime();
//
//            Long now = System.currentTimeMillis();
//            if(now > expire){
//               SessionUtils.remove();
//                throw  new NoAuthExpetion(new Exception("未登录或者已经过期"));
//            }
//
////            String path = request.getContextPath();
//
//            Claim payload = m.get("user");
//            Map<String,Object> map = payload.asMap();
//            Object oid = map.get("id");
//            SessionUtils.setUserId(Long.parseLong(oid.toString()));
//        }

        return true;
    }
}